Jazzila.prototype.search = function()
{
	this.nsIXmppService.search("", "", "", "", "");
}

Jazzila.prototype.openStatusWindow = function ()
{
	this.openWindow("chrome://jazzila/content/presence.xul", "Change status");
}

Jazzila.prototype.disableMenuItems = function ()
{
	var ids = ["jazzila-context-menu-connect", "jazzila-context-menu-disconnect", "jazzila-context-menu-status", "jazzila-context-menu-join", "jazzila-context-menu-subscribe", "jazzila-context-menu-search", "jazzila-actions-separator"];
	for (var i = 0; i < ids.length; i++)			
	{
		var menuitem=document.getElementById(ids[i]);
		if (menuitem)
		{
			var state = menuitem.getAttribute("disabled");
			menuitem.setAttribute("disabled", (state=="true" ? "false" : "true"));
		}
	}
}

Jazzila.prototype.onStateChange = function (state)
{
	var status=document.getElementById("jazzila-icon");
	if (status)
	{
		if (state>STATE_CONNECTED && !this.isConnected)
		{
			this.disableMenuItems();
			status.setAttribute("show", "available");
			this.isConnected = true;
		}
		if (state<STATE_CONNECTED && this.isConnected){
			this.disableMenuItems();
			status.setAttribute("show", "none");
			this.isConnected = false;
		}
	}
}

Jazzila.prototype.openContextWindow = function ()
{

	if (this.hasNewMessages)
	{

	}
	else
	{
		var win = this.openWindow("chrome://jazzila/content/roster.xul", "Roster");
		win.document.title = "Presence";
		win.focus();
	}
}

Jazzila.prototype.onButtonClick = function (event)
{
	switch (event.button)
	{
	case 0:
		this.openContextWindow();
		break;
	case 2:
		this.showContextMenu(event.clientX, event.clientY, "jazzila-context-menu", "jazzila-icon");		
		break;
	};
}

Jazzila.prototype.disconnect = function (){
	this.nsIXmppService.disConnect();
}

Jazzila.prototype.sendPresence = function (event)
{
	var show = event.target;
	if (show)
	{
		if (show.value !="custom")
			this.nsIXmppService.setLineStatus(show.value, null);
		else 
			this.openStatusWindow();
	}
}

Jazzila.prototype.subscribe = function ()
{
    var contact = window.prompt("Add contact", "Add contact:", contact, null, null);
    if (contact)
    {
	if (!contact.match(/.*@.*/))
	{
		contact = contact +"@" + this.domain;
	}
	this.nsIXmppService.subscribe(contact);
    }
}

Jazzila.prototype.join = function ()
{
    this.rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
    this.cu = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
    var ds = this.getDataSource();
    var ctn = this.cu.MakeSeq(ds,this.rdf.GetResource("urn:xmpp:identity:category:conference:text"));
    var confjid = this.domain;
    var iter = ctn.GetElements();
    if (iter.hasMoreElements())
    {
		confjid =  getRDFLiteral(ds, iter.getNext(), this.rdf.GetResource("urn:xmpp:identity#jid"));
    }	
    var room = window.prompt("Join group chat", "", null, null, null);
    if (!room) return;
	if (!room.match(/.*@.*/))
	{
		room = room +"@" + confjid;
	}
	var nick = window.prompt("use nickname", this.user, null, null, null);
	if(!nick) return;
	var param = CC["@mozilla.org/supports-string;1"].createInstance(CI.nsISupportsString);
	param.data = nick;
    var win = this.openWindow("chrome://jazzila/content/groupchat.xul", room, param);
	win.document.title = room;
	win.focus();
}
